/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package midi;

import java.util.Vector;

/**
 *
 * @author TJ
 */
public class DrumGroove implements Runnable{
    private int division=2;
    private int tempo=120;
    private int time=4;
    private int looping=0;
    private boolean kill=true;
    private boolean[] accents;
    private MidiPlayer play;
    private Vector<Integer>[] notes;
    DrumGroove(MidiPlayer p){
        resetMeasure();
        play=p;
    }
    private void resetMeasure() {
        accents=new boolean[division*time];
        notes=new Vector[division*time];
        for(int i=0; i<division*time;i++) {
            accents[i]=false;
            notes[i]=new Vector<Integer>();
        }
    }
    private void play(int loops){
        looping=loops;
        
    }
    public void play() {
        if(looping==0) {
            play(1);
        }
    }
    public void loop() {
        if(looping==0) {
            play(-1);
        }
    }
    public void stop(){
        looping=0;
    }
    @Override
    public void run(){
        long sleepTime=(long)(1000/(tempo/60));
        try{
            int i=0;
            kill=false;
            while(looping!=0){
                for(Integer i2 :notes[i]) {
                    int volume=accents[i]?120:90;  
                    play.playNote(10, i2, volume);
                }
                Thread.sleep(sleepTime/division);
                for(Integer i2 :notes[i]) {
                    play.stopNote(10, i2);
                }
                if(kill){break;}
                i++;
                if(i==notes.length) {
                    i=0;
                    looping--;
                }
            }
             kill=true;
        }catch(Exception e){}
    }
}
